package cn.neusoft.service.impl;

import cn.neusoft.mapper.OrderDetailMapper;
import cn.neusoft.mapper.OrderMapper;
import cn.neusoft.pojo.Order;
import cn.neusoft.pojo.OrderDetail;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.OrderQuery;
import cn.neusoft.service.IOrderService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderService implements IOrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    /**
     *
     * @param order  如果参数值传递一个order对象  那么订单详情的集合就需要放在Order身上
     * @return
     */
    @Transactional
    @Override
    public ResultBean add(Order order) {

        if (order==null ||
                order.getUserId()==null || order.getUserId()==0 ||
                order.getBusinessId()==null || order.getBusinessId()==0 ||
                order.getAddressId()==null || order.getAddressId()==0){
            return ResultBean.ERROR("请求参数为空值");
        }

        order.setOrderDate(new Date());//设置下单时间为当前日期时间
        order.setOrderState(0); //设置未支付的订单状态

        //使用uuid生成订单编号
        order.setOrderNumber(UUID.randomUUID().toString().replaceAll("-",""));

        //判断是否存在总价  不存在就计算并设置
        if(order.getOrderTotal()==null){
            //遍历订单详情
            List<OrderDetail> orderDetailList = order.getOrderDetailList();
            double allPrice = 0;
            for (OrderDetail detail:orderDetailList) {
                allPrice += detail.getFood().getFoodPrice()*detail.getQuantity();
            }
            order.setOrderTotal(allPrice);
        }


        int add = orderMapper.add(order);

        //订单添加成功
        if (add>0){

            //循环将订单编号设置给每一个订单详情对象
            List<OrderDetail> orderDetailList = order.getOrderDetailList();
            for (OrderDetail detail:orderDetailList) {
                detail.setOrderId(order.getOrderId());
            }
            //批量添加订单详情
            int i = orderDetailMapper.addBatch(order.getOrderDetailList());
            //判断订单详情是否添加成功
            if (i==order.getOrderDetailList().size()){
                return ResultBean.SUCCESS();
            }
        }

        return ResultBean.ERROR("添加订单信息失败");
    }

    @Transactional
    @Override
    public ResultBean update(Order order) {

        if (order==null ||
                order.getOrderId()==null || order.getOrderId()==0 ||
                order.getUserId()==null || order.getUserId()==0 ||
                order.getBusinessId()==null || order.getBusinessId()==0 ||
                order.getAddressId()==null || order.getAddressId()==0  ||
                order.getOrderDate()==null ||
                order.getOrderTotal()==null || order.getOrderTotal()==0
        ){
            return ResultBean.ERROR("请求参数为空值");
        }

        int update = orderMapper.update(order);

        return update>0?ResultBean.SUCCESS():ResultBean.ERROR("修改订单信息失败");
    }

    /**
     * 删除订单时  同时删除关联的订单详情
     * @param id  订单编号  orderId
     * @return
     */
    @Transactional
    @Override
    public ResultBean delete(Integer id) {

        if (id==null){
            return ResultBean.ERROR("请求参数为空值");
        }

        int delete = orderMapper.delete(id);

        if (delete==0){
            throw new RuntimeException("删除订单失败"+id);
        }

        int i = orderDetailMapper.deleteByOrderId(id);
        if (i==0){
            throw new RuntimeException("删除订单详情失败"+id);
        }

        return ResultBean.SUCCESS();
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) {

        if (id==null){
            return ResultBean.ERROR("请求参数为空值");
        }

        Order order = orderMapper.getById(id);
        return order!=null?ResultBean.SUCCESS(order):ResultBean.ERROR("查询订单信息失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() {
        List<Order> orderList = orderMapper.getAll();
        return ResultBean.SUCCESS(orderList);
    }

    /**
     * 分页查询
     * @param baseQuery
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) {

        if (baseQuery == null){
            baseQuery = new OrderQuery();
        }

        PageBean<Order> pageBean = new PageBean<>();
        int count = orderMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        List<Order> orderList = orderMapper.getByPage(baseQuery);
        pageBean.setData(orderList);

        return ResultBean.SUCCESS(pageBean);
    }

    /**
     * 批量删除订单
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids) {

        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("请求参数为空值");
        }

        //遍历要删除的订单id
        for (Integer id:ids){
            delete(id);
        }

        return ResultBean.SUCCESS();
    }

    /**
     * 根据订单编号查询对应的订单详情
     * @param orderId
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public ResultBean getDetailsByOrderId(Integer orderId) {
        if (orderId==null || orderId==0){
            return ResultBean.ERROR("参数为空");
        }
        List<OrderDetail> orderDetails = orderDetailMapper.getByOrderId(orderId);
        return ResultBean.SUCCESS(orderDetails);
    }

    @Override
    public ResultBean getOrderByUserId(Integer userId) {

        if (userId==null || userId==0){
            return ResultBean.ERROR("参数为空");
        }

        List<Order> orderList = orderMapper.getOrderByUserId(userId);

        return ResultBean.SUCCESS(orderList);
    }
}
